package com.shushan.pages;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.Constants;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.exam.ExamActivity;
import com.shushan.exercise.SyncExerciseActivity;
import com.shushan.home.adapter.AwardToyListAdapter;
import com.shushan.home.bean.RewardToyBean;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetListCommListener;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.ui.VerificationCodeView;
import com.shushan.util.Session;

import java.util.List;

/**
 * @ClassName: GetToysActivity
 * @Desciption: 练习获得礼物
 * @author: yichaohua
 * @date: 2018-07-21
 */
public class GetToysActivity extends NavigationLoseActivity implements AwardToyListAdapter.OnStartButtonClickListener, AdapterView.OnItemClickListener {

    private static final String TAG = "GetToysActivity";

    private ListView listView;
    private AwardToyListAdapter toyAdapter;
    private Dialog noVipDialog;

    private BabyApplication shuShanApplication = BabyApplication.getInstance();
    private List<RewardToyBean> rewardToyBeans;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.get_toys_activity);

        setTitle("练习获得礼物");

        initView();

        getData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(noVipDialog != null){
            noVipDialog.dismiss();
        }
    }

    private void initView() {
        listView = findViewById(R.id.get_toys_lv);
        listView.setOnItemClickListener(this);
    }

    private void getData() {
        int childId = shuShanApplication.getCurrentChildId();
        showWaitDialog();

        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.GET_CHILD_TOYS);
        UrlParse paramParse = new UrlCommParse(this)
                .putValue("child_id", childId);
        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(paramParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetListCommListener<RewardToyBean>() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(List<RewardToyBean> rewardToyBeanList) {
                        if(rewardToyBeanList != null){
                            rewardToyBeans = rewardToyBeanList;
                            toyAdapter = new AwardToyListAdapter(GetToysActivity.this, rewardToyBeanList);
                            toyAdapter.setOnStartButtonClickListener(GetToysActivity.this);
                            listView.setAdapter(toyAdapter);
                        }

                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                    }
                });
    }

    @Override
    public void onStartButtonClick(View itemView, int position) {
        if(!shuShanApplication.isVip()){
            showNoVipDialog();
            return;
        }

        RewardToyBean rewardToyBean = rewardToyBeans.get(position);
        switch (rewardToyBean.practice_type) {
            case Constants.TYPE_SMART_EXERCISE:
                startSmartExam(rewardToyBean);
                break;
            case Constants.TYPE_SYNC_EXERCISE:
                Session.getSession().put("rewardToyBean",rewardToyBean);
                Intent intent = new Intent(this,SyncExerciseActivity.class);
                intent.putExtra("awardType", 2);
                startActivityAllow(intent);
                break;
            case Constants.TYPE_KEY_POINT_EXERCISE:
            case Constants.TYPE_DIFFICULT_POINT_EXERCISE:
            case Constants.TYPE_EASY_WRONG_EXERCISE:
            case Constants.TYPE_SPECIAL_TOPIC_EXERCISE:
                Session.getSession().put("rewardToyBean",rewardToyBean);
                Intent intent1 = new Intent(this,SpecialTopicExerciseActivity.class);
                intent1.putExtra("awardType", 2);
                startActivityAllow(intent1);
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(!shuShanApplication.isVip()){
            showNoVipDialog();
            return;
        }
        RewardToyBean rewardToyBean = rewardToyBeans.get(position);
        Session.getSession().put("rewardToyBean",rewardToyBean);

        startActivity(ToyAwardDetailsActivity.class);

    }

    private void startSmartExam(RewardToyBean rewardToyBean) {
        if(rewardToyBean == null){
            showToast("智能练习玩具奖励信息为空");
            return;
        }

        if(!rewardToyBean.znlx){
            showToast("智能练习暂无题目");
            return;
        }

        Session.getSession().put("rewardToyBean",rewardToyBean);

        Intent intent = new Intent(this,ExamActivity.class);
        intent.putExtra("awardType",2);
        startActivityAllow(intent);
    }

    private void showNoVipDialog() {
        if(noVipDialog == null){
            noVipDialog = new Dialog(this,R.style.Dialog_Translucent_Background);
            noVipDialog.setCanceledOnTouchOutside(true);
            noVipDialog.setContentView(R.layout.no_vip_dialog);

            Button openButton = noVipDialog.findViewById(R.id.no_vip_open_btn);
            openButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    noVipDialog.dismiss();

                    startActivity(OpenVipSpendActivity.class);
                }
            });

            Button shareButton = noVipDialog.findViewById(R.id.no_vip_share_btn);
            shareButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    noVipDialog.dismiss();

                    startActivity(InviteForFreeUseActivity.class);
                }
            });

        }

        if(!noVipDialog.isShowing()){
            noVipDialog.show();
        }
    }
}
